﻿using System;
using System.Windows.Forms;
using DC2010.Objects.Data;
using DC2010.Tools;
using DC2010;

namespace DC2010_EditorInfo
{
    public partial class ItemsGfxForm : Form
    {
        public ItemGfx ItemGfx { get; set; }
        public string OriginalId { get; set; }


        public ItemsGfxForm()
        {
            InitializeComponent();

            ItemGfx = new ItemGfx();
        }

        private void ItemsForm_Load(object sender, EventArgs e)
        {
            InitializeLB();

            gfxPlacement_0_0.Init(0, 0, 0, 0, "", 100);
            gfxPlacement_0_m1.Init(0, -1, 0, 0, "", 100);
            gfxPlacement_0_m2.Init(0, -2, 0, 0, "", 100);
            gfxPlacement_0_m3.Init(0, -3, 0, 0, "", 100);
            gfxPlacement_0_m4.Init(0, -4, 0, 0, "", 100);
            gfxPlacement_0_m5.Init(0, -5, 0, 0, "", 100);


            gfxPlacement_m1_0.Init(-1, 0, 0, 0, "", 100);
            gfxPlacement_m1_m1.Init(-1, -1, 0, 0, "", 100);
            gfxPlacement_m1_m2.Init(-1, -2, 0, 0, "", 100);
            gfxPlacement_m1_m3.Init(-1, -3, 0, 0, "", 100);
            gfxPlacement_m1_m4.Init(-1, -4, 0, 0, "", 100);
            gfxPlacement_m1_m5.Init(-1, -5, 0, 0, "", 100);


            gfxPlacement_m2_m2.Init(-2, -2, 0, 0, "", 100);
            gfxPlacement_m2_m3.Init(-2, -3, 0, 0, "", 100);
            gfxPlacement_m2_m4.Init(-2, -4, 0, 0, "", 100);
            gfxPlacement_m2_m4.Init(-2, -5, 0, 0, "", 100);

        }

        private void InitializeLB()
        {
            imageListBox1.Items.Clear();

            foreach (ItemGfx ei in Root.I.ItemSystem.DictGfx.Values)
            {
                ImageListBoxItem ilbi = new ImageListBoxItem(ei.Id, Properties.Resources.Item);
                imageListBox1.Items.Add(ilbi);
            }
        }

        private void crystalButton1_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(OriginalId))
            {
                //we are updating
                GatherInfo(ItemGfx);
                Root.I.ItemSystem.SaveGfx();
                InitializeLB();
            }
            else
            {
                //creating new
                GatherInfo(ItemGfx);
                Root.I.ItemSystem.AddGfx(ItemGfx);
                Root.I.ItemSystem.SaveGfx();
                InitializeLB();
            }
        }

        private void GatherInfo(ItemGfx ei)
        {
            ei.Id = IdTB.Text;

            ei.Icon.Texture = IconTB.Text;
            ei.Floor.Texture = FloorTB.Text;
            ei.Side.Texture = SideTB.Text;
            ei.Towards.Texture = TowardsTB.Text;
            ei.Away.Texture = AwayTB.Text;

            // 0
            ei.pos_0_0.Scale = gfxPlacement_0_0.Scl;
            ei.pos_0_0.X = gfxPlacement_0_0.posX;
            ei.pos_0_0.Y = gfxPlacement_0_0.posY;
            ei.pos_0_0.Texture = gfxPlacement_0_0.Path;

            ei.pos_0_m1.Scale = gfxPlacement_0_m1.Scl;
            ei.pos_0_m1.X = gfxPlacement_0_m1.posX;
            ei.pos_0_m1.Y = gfxPlacement_0_m1.posY;
            ei.pos_0_m1.Texture = gfxPlacement_0_m1.Path;

            ei.pos_0_m2.Scale = gfxPlacement_0_m2.Scl;
            ei.pos_0_m2.X = gfxPlacement_0_m2.posX;
            ei.pos_0_m2.Y = gfxPlacement_0_m2.posY;
            ei.pos_0_m2.Texture = gfxPlacement_0_m2.Path;

            ei.pos_0_m3.Scale = gfxPlacement_0_m3.Scl;
            ei.pos_0_m3.X = gfxPlacement_0_m3.posX;
            ei.pos_0_m3.Y = gfxPlacement_0_m3.posY;
            ei.pos_0_m3.Texture = gfxPlacement_0_m3.Path;

            ei.pos_0_m4.Scale = gfxPlacement_0_m4.Scl;
            ei.pos_0_m4.X = gfxPlacement_0_m4.posX;
            ei.pos_0_m4.Y = gfxPlacement_0_m4.posY;
            ei.pos_0_m4.Texture = gfxPlacement_0_m4.Path;

            ei.pos_0_m5.Scale = gfxPlacement_0_m5.Scl;
            ei.pos_0_m5.X = gfxPlacement_0_m5.posX;
            ei.pos_0_m5.Y = gfxPlacement_0_m5.posY;
            ei.pos_0_m5.Texture = gfxPlacement_0_m5.Path;

            // -1
            ei.pos_m1_0.Scale = gfxPlacement_m1_0.Scl;
            ei.pos_m1_0.X = gfxPlacement_m1_0.X;
            ei.pos_m1_0.Y = gfxPlacement_m1_0.Y;
            ei.pos_m1_0.Texture = gfxPlacement_m1_0.Path;

            ei.pos_m1_m1.Scale = gfxPlacement_m1_m1.Scl;
            ei.pos_m1_m1.X = gfxPlacement_m1_m1.posX;
            ei.pos_m1_m1.Y = gfxPlacement_m1_m1.posY;
            ei.pos_m1_m1.Texture = gfxPlacement_m1_m1.Path;

            ei.pos_m1_m2.Scale = gfxPlacement_m1_m2.Scl;
            ei.pos_m1_m2.X = gfxPlacement_m1_m2.posX;
            ei.pos_m1_m2.Y = gfxPlacement_m1_m2.posY;
            ei.pos_m1_m2.Texture = gfxPlacement_m1_m2.Path;

            ei.pos_m1_m3.Scale = gfxPlacement_m1_m3.Scl;
            ei.pos_m1_m3.X = gfxPlacement_m1_m3.posX;
            ei.pos_m1_m3.Y = gfxPlacement_m1_m3.posY;
            ei.pos_m1_m3.Texture = gfxPlacement_m1_m3.Path;

            ei.pos_m1_m4.Scale = gfxPlacement_m1_m4.Scl;
            ei.pos_m1_m4.X = gfxPlacement_m1_m4.posX;
            ei.pos_m1_m4.Y = gfxPlacement_m1_m4.posY;
            ei.pos_m1_m4.Texture = gfxPlacement_m1_m4.Path;

            ei.pos_m1_m5.Scale = gfxPlacement_m1_m5.Scl;
            ei.pos_m1_m5.X = gfxPlacement_m1_m5.posX;
            ei.pos_m1_m5.Y = gfxPlacement_m1_m5.posY;
            ei.pos_m1_m5.Texture = gfxPlacement_m1_m5.Path;


            // -2
            ei.pos_m2_m2.Scale = gfxPlacement_m2_m2.Scl;
            ei.pos_m2_m2.X = gfxPlacement_m2_m2.posX;
            ei.pos_m2_m2.Y = gfxPlacement_m2_m2.posY;
            ei.pos_m2_m2.Texture = gfxPlacement_m2_m2.Path;

            ei.pos_m2_m3.Scale = gfxPlacement_m2_m3.Scl;
            ei.pos_m2_m3.X = gfxPlacement_m2_m3.posX;
            ei.pos_m2_m3.Y = gfxPlacement_m2_m3.posY;
            ei.pos_m2_m3.Texture = gfxPlacement_m2_m3.Path;

            ei.pos_m2_m4.Scale = gfxPlacement_m2_m4.Scl;
            ei.pos_m2_m4.X = gfxPlacement_m2_m4.posX;
            ei.pos_m2_m4.Y = gfxPlacement_m2_m4.posY;
            ei.pos_m2_m4.Texture = gfxPlacement_m2_m4.Path;

            ei.pos_m2_m5.Scale = gfxPlacement_m2_m5.Scl;
            ei.pos_m2_m5.X = gfxPlacement_m2_m5.posX;
            ei.pos_m2_m5.Y = gfxPlacement_m2_m5.posY;
            ei.pos_m2_m5.Texture = gfxPlacement_m2_m5.Path;
        }

        private void FillWithObject()
        {
            if (ItemGfx != null)
            {
                OriginalId = ItemGfx.Id;
                IdTB.Text = ItemGfx.Id;

                IconTB.Text = ItemGfx.Icon.Texture;
                FloorTB.Text = ItemGfx.Floor.Texture;
                SideTB.Text = ItemGfx.Side.Texture;
                TowardsTB.Text = ItemGfx.Towards.Texture;
                AwayTB.Text = ItemGfx.Away.Texture;


                gfxPlacement_0_0.Init(0, 0, ItemGfx.pos_0_0);
                gfxPlacement_0_m1.Init(0, -1, ItemGfx.pos_0_m1);
                gfxPlacement_0_m2.Init(0, -2, ItemGfx.pos_0_m2);
                gfxPlacement_0_m3.Init(0, -3, ItemGfx.pos_0_m3);
                gfxPlacement_0_m4.Init(0, -4, ItemGfx.pos_0_m4);
                gfxPlacement_0_m5.Init(0, -5, ItemGfx.pos_0_m5);

                gfxPlacement_m1_0.Init(-1, 0, ItemGfx.pos_m1_0);
                gfxPlacement_m1_m1.Init(-1, -1, ItemGfx.pos_m1_m1);
                gfxPlacement_m1_m2.Init(-1, -2, ItemGfx.pos_m1_m2);
                gfxPlacement_m1_m3.Init(-1, -3, ItemGfx.pos_m1_m3);
                gfxPlacement_m1_m4.Init(-1, -4, ItemGfx.pos_m1_m4);
                gfxPlacement_m1_m5.Init(-1, -5, ItemGfx.pos_m1_m5);

                gfxPlacement_m2_m2.Init(-2, -2, ItemGfx.pos_m2_m2);
                gfxPlacement_m2_m3.Init(-2, -3, ItemGfx.pos_m2_m3);
                gfxPlacement_m2_m4.Init(-2, -4, ItemGfx.pos_m2_m4);
                gfxPlacement_m2_m5.Init(-2, -5, ItemGfx.pos_m2_m5);

            }
        }

        private void imageListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = ((ImageListBoxItem)imageListBox1.SelectedItem).Text;
            ItemGfx = Root.I.ItemSystem.GetGfx(s);
            FillWithObject();
        }

        private void crystalButton2_Click(object sender, EventArgs e)
        {
            ItemGfx = new ItemGfx();
            FillWithObject();
            OriginalId = "";
        }
    }
}
